import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

public class SallyToLibSVM {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		HashMap<String, String> label = new HashMap<String, String>();
		label.put("doisong", "1");
		label.put("giaitri", "2");
		label.put("kinhdoanh", "3");
		label.put("sohoa", "4");
		label.put("thethao", "5");
		
		ArrayList<String> fileName = new ArrayList<String>();
		fileName.add("1_2");
		fileName.add("1_3");
		fileName.add("1_4");
		fileName.add("1_5");
		fileName.add("2_3");
		fileName.add("2_4");
		fileName.add("2_5");
		fileName.add("3_4");
		fileName.add("3_5");
		fileName.add("4_5");
		
//		for (int i = 0; i < fileName.size(); i++) {
		convertToLibSVM2(label, "test");
//			convertToLibSVM4(label, "1_2_3_4_5", 5, 15000, 1000);
//			convertToLibSVM5(label, "1_2_3_4_5");
//			convertToLibSVM6(label, "1_2_3_4_5", 5, 15000, 1000);
//		}
			
//			convertToLibSVM4(label, "1_2_3_4_5", 5, 15000, 1000);
			
//		convertToLibSVM2(label, "1_2");
//		convertToLibSVM3(label, "1_2");
//		convertToLibSVM4(label, "1_2",2);

	}
	
	static void convertToLibSVM(HashMap<String, String> label, String fileName){
		List<String> arrayFiles = new LinkedList<String>();
		String fileOuputName = fileName + ".train";
		String fileInputName = "a_" + fileOuputName;
		
		try {
			File fileInput = new File(fileInputName);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					new FileInputStream(fileInput), "UTF8"));

			File fileOutput = new File(fileOuputName);
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(fileOutput), "UTF8"));

			String str;
			String docLabel;

			while ((str = in.readLine()) != null) {
				if (str.contains("#")) {
					docLabel = str.substring(str.indexOf("#"), str.length());
					if (label.containsKey(docLabel.split(" ")[1])) {
						str = label.get(docLabel.split(" ")[1])
								+ str.substring(str.indexOf(" "),
										str.indexOf("#")) + "\n";
						arrayFiles.add(str);
					}					
				}
				
				System.out.println(str);
			}

			in.close();
			Collections.sort(arrayFiles);
			for(int i = 0 ; i < arrayFiles.size(); i++){
				out.append(arrayFiles.get(i));
				out.flush();
			}
			
			out.close();
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
	static void convertToLibSVM2(HashMap<String, String> label, String fileInputName){
		List<String> arrayFiles = new LinkedList<String>();
		String fileOuputName = fileInputName + ".train";
		fileInputName = fileInputName + ".libsvm";
		
		try {
			File fileInput = new File(fileInputName);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					new FileInputStream(fileInput), "UTF8"));

			File fileOutput = new File(fileOuputName);
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(fileOutput), "UTF8"));

			String str;
			String docLabel;

			while ((str = in.readLine()) != null) {
				if (str.contains("#")) {
					docLabel = str.substring(str.indexOf("#"), str.length());
					if (label.containsKey(docLabel.split(" ")[1])) {
						str = label.get(docLabel.split(" ")[1])
								+ str.substring(str.indexOf(" "),
										str.indexOf("#")) + "\n";
						arrayFiles.add(str);
					}					
				}
				
				System.out.println(str);
			}

			in.close();
			Collections.sort(arrayFiles);
			for(int i = 0 ; i < arrayFiles.size(); i++){
				out.append(arrayFiles.get(i));
				out.flush();
			}
			
			out.close();
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
	static void convertToLibSVM3(HashMap<String, String> label, String fileInputName){
		List<String> arrayFiles = new LinkedList<String>();
		String fileOuputName = fileInputName + ".out";
		fileInputName = fileInputName + ".train";
		System.out.println("fileInputName : " + fileInputName);
		try {
			File fileInput = new File(fileInputName);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					new FileInputStream(fileInput), "UTF8"));

			File fileOutput = new File(fileOuputName);
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(fileOutput), "UTF8"));

			String str;
			String docLabel;

			while ((str = in.readLine()) != null) {
				if (str.contains(" ")) {
					System.out.println("str.split(\" \")[0] : " + str.split(" ")[0]);
					if(str.split(" ")[0].equals("3")) {
						str = 1 + str.substring(str.indexOf(" "), str.length());
					}
					if(str.split(" ")[0].equals("4") || str.split(" ")[0].equals("5")) {
						str = 2 + str.substring(str.indexOf(" "), str.length());
					}					
					arrayFiles.add(str);					
				} 
				
				System.out.println(str);
			}

			in.close();
			Collections.sort(arrayFiles);
			for(int i = 0 ; i < arrayFiles.size(); i++){
				out.append(arrayFiles.get(i));
				out.flush();
			}
			
			out.close();
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
	//read Sally and write to 2 file: train & test
	static void convertToLibSVM4(HashMap<String, String> label, String fileInputName, int numLabel, int totalArticle, int numTest){
		List<String> arrayFiles = new LinkedList<String>();
		String fileOuputName1 = fileInputName + ".train";
		String fileOuputName2 = fileInputName + ".test";
		fileInputName = fileInputName + ".libsvm";
		
		try {
			File fileInput = new File(fileInputName);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					new FileInputStream(fileInput), "UTF8"));

			File fileOutput1 = new File(fileOuputName1);
			BufferedWriter out1 = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(fileOutput1), "UTF8"));
			
			File fileOutput2 = new File(fileOuputName2);
			BufferedWriter out2 = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(fileOutput2), "UTF8"));

			String str;
			String docLabel;

			while ((str = in.readLine()) != null) {
				if (str.contains("#")) {
					docLabel = str.substring(str.indexOf("#"), str.length());
					if (label.containsKey(docLabel.split(" ")[1])) {
						str = label.get(docLabel.split(" ")[1])
								+ str.substring(str.indexOf(" "),
										str.indexOf("#")) + "\n";
						arrayFiles.add(str);
					}					
				}
				
				System.out.println(str);
			}

			in.close();
			Collections.sort(arrayFiles);
			//write to train file
			for(int i = 0 ; i < numLabel; i++){
				for(int j = i * totalArticle; j < (i+1) * totalArticle - numTest; j++){
					out1.append(arrayFiles.get(j));
					out1.flush();
				}				
			}
			out1.close();
			
			//write to test file
			for(int i = 0 ; i < numLabel; i++){
				for(int j = i * totalArticle + (totalArticle - numTest); j < (i+1) * totalArticle; j++){
					out2.append(arrayFiles.get(j));
					out2.flush();
				}				
			}
			out2.close();
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
	//read Sally and write to 1 file
		static void convertToLibSVM5(HashMap<String, String> label, String fileInputName){
			List<String> arrayFiles = new LinkedList<String>();
			String fileOuputName1 = fileInputName + ".svm";
			fileInputName = fileInputName + ".svm.scale";
			
			try {
				File fileInput = new File(fileInputName);
				BufferedReader in = new BufferedReader(new InputStreamReader(
						new FileInputStream(fileInput), "UTF8"));

				File fileOutput1 = new File(fileOuputName1);
				BufferedWriter out1 = new BufferedWriter(new OutputStreamWriter(
						new FileOutputStream(fileOutput1), "UTF8"));
				
				String str;
//				String docLabel;

				while ((str = in.readLine()) != null) {
//					docLabel = str.substring(0, 1);
					
					arrayFiles.add(str + "\n");
					
					System.out.println(str);
				}

				in.close();
				Collections.sort(arrayFiles);
				//write to train file
				for(int i = 0 ; i < arrayFiles.size(); i++){
					out1.append(arrayFiles.get(i));
					out1.flush();			
				}
				out1.close();
				
			} catch (UnsupportedEncodingException e) {
				System.out.println(e.getMessage());
			} catch (IOException e) {
				System.out.println(e.getMessage());
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
	
	//read Sally and write to 2 file: train & test //using after scale
		static void convertToLibSVM6(HashMap<String, String> label, String fileInputName, int numLabel, int totalArticle, int numTest){
			List<String> arrayFiles = new LinkedList<String>();
			String fileOuputName1 = fileInputName + ".train";
			String fileOuputName2 = fileInputName + ".test";
			fileInputName = fileInputName + ".libsvm.scale";
			
			try {
				File fileInput = new File(fileInputName);
				BufferedReader in = new BufferedReader(new InputStreamReader(
						new FileInputStream(fileInput), "UTF8"));

				File fileOutput1 = new File(fileOuputName1);
				BufferedWriter out1 = new BufferedWriter(new OutputStreamWriter(
						new FileOutputStream(fileOutput1), "UTF8"));
				
				File fileOutput2 = new File(fileOuputName2);
				BufferedWriter out2 = new BufferedWriter(new OutputStreamWriter(
						new FileOutputStream(fileOutput2), "UTF8"));

				String str;
				String docLabel;

				while ((str = in.readLine()) != null) {
					arrayFiles.add(str + "\n");
					
					System.out.println(str);
				}

				in.close();
				Collections.sort(arrayFiles);
				//write to train file
				for(int i = 0 ; i < numLabel; i++){
					for(int j = i * totalArticle; j < (i+1) * totalArticle - numTest; j++){
						out1.append(arrayFiles.get(j));
						out1.flush();
					}				
				}
				out1.close();
				
				//write to test file
				for(int i = 0 ; i < numLabel; i++){
					for(int j = i * totalArticle + (totalArticle - numTest); j < (i+1) * totalArticle; j++){
						out2.append(arrayFiles.get(j));
						out2.flush();
					}				
				}
				out2.close();
			} catch (UnsupportedEncodingException e) {
				System.out.println(e.getMessage());
			} catch (IOException e) {
				System.out.println(e.getMessage());
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}

	static String readFile(String fileName) {
		String temp = "";
		try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
			String sCurrentLine;
			while ((sCurrentLine = br.readLine()) != null) {
				temp += sCurrentLine + "\n";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return temp;
	}

	static void writeFile(String fileName, String writeString) {
		try {
			File fileDir = new File(fileName);
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(fileDir), "UTF8"));

			out.append(writeString);

			out.flush();
			out.close();
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}
